<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\common\controller\ServerResponse;
use app\model\Blogs;
use app\model\Hot_update;
use think\Request;

class Blog
{
    //编辑文章
    public function editBlog(Request $request){
        $title = $request->post('title');//标题
        $content=$request->post('content');//富文本
        $tid = $request->post('tid');//栏目tid
        $wid = $request->post('wid');//发布类型wid
        $url = $request->post('url');//图片路径
        $bid = $request->post('bid');
        $uploadadmin='沐歌';
        $Blog=Blogs::where('bid','=',$bid)->find();
        $data=['title'=>$title,
            'detail'=>$content,
            'wid'=>$wid,
            'uploadadmin'=>$uploadadmin,
            'pic'=>$url,
            'tid'=>$tid
            ];
        $Blog->save($data);
        $hot=Hot_update::hotUpdate(1);
        if($data&&$hot){
            return ServerResponse::jsonData(1,'更新成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'更新失败！',$data);
        }
    }
    //获取文章详情
    public function getBlogDetail(Request $request){
        $bid = $request->post('bid');
        $data = Blogs::where('bid','=',$bid)->find();
        $data['url']=$data['pic'];
        $data['pic']=DOMAIN_NAME.$data['pic'];
        if($data){
            return ServerResponse::jsonData(1,'获取成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'获取失败！',$data);
        }
}
    //获取文章列表
    public function getBlogList(Request $request){
        $limit = $request->post('limit')??10;
        $page = $request->post('page')??1;
        $keyword=$request->post('keyword');
        $Blog=new Blogs();
        $res = $Blog->getBlogList($page,$limit,$keyword);
        if($res['data']){
            return ServerResponse::jsonData(1,'获取成功!',$res);
        }else{
            return ServerResponse::jsonData(0,'获取失败',$res);
        }
    }
    //添加文章
    public function addBlog(Request $request){
        $title = $request->post('title');//标题
        $content=$request->post('content');//富文本
        $tid = $request->post('tid');//栏目tid
        $wid = $request->post('wid');//发布类型wid
        $uploadadmin='沐歌';
        $url=$request->post('url');//图片路径
        $data=Blogs::create([
            'wid'=>$wid,
            'tid'=>$tid,
            'title'=>$title,
            'uploaddate'=>time(),
            'uploadadmin'=>$uploadadmin,
            'visited'=>rand(1,202),
            'detail'=>$content,
            'pic'=>$url
        ]);
        $hot=Hot_update::hotUpdate(1);
        if($data&&$hot){
            return ServerResponse::jsonData(1,'添加成功！',$data);
        }else{
            return ServerResponse::jsonData(0,'添加失败！',$data);
        }
    }
    //删除文章
    public function delBlog(Request $request){
        $bid = $request->post('bid');
        $data = Blogs::where('bid','=',$bid)->delete();
        $hot=Hot_update::hotUpdate(1);
        if($data&&$hot){
            return ServerResponse::jsonData(1,'删除成功!',null);
        }else{
            return ServerResponse::jsonData(0,'删除失败',$data);
        }
    }
}
